package common;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.SortedMap;


public class CommonHelper {
   
    public static void printToFile(String filename, String content) {
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(filename));
            out.write(content);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }       
    }
    
    public static String join(Collection<?> s, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        Iterator<?> iter = s.iterator();
        while (iter.hasNext()) {
            buffer.append(iter.next());
            if (iter.hasNext()) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }

    public static String HvInTimeToStr(SortedMap<Long, Double> hvInTime) {
        
        StringBuffer sb = new StringBuffer();
        for (Long key : hvInTime.keySet()) {
            Double value = hvInTime.get(key);
            sb.append(key+", "+value+"\n");
        }
        return sb.toString();
    }
    
    /**
     * Zwykły sleep
     */
    public static void sleep(int mili) {
        try {
            Thread.sleep(mili);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * Na podstawie nazwy pliku wejściowego tworzy prefiks dla plików wyjściowych.
     * @param inputFileName
     * @return
     */
    public static String fromInputfilenameToPrefix(String inputFileName) {
        
        String resultFilePrefix = inputFileName;
        int dotPos = inputFileName.lastIndexOf(".");
        int lastslashPos = inputFileName.lastIndexOf("/");
        if (dotPos != -1) {
            resultFilePrefix = resultFilePrefix.substring(lastslashPos+1, dotPos);
        }
        resultFilePrefix = resultFilePrefix.replaceAll("input", "output");
        return resultFilePrefix;
    }

    /**
     * Zwraca ile elementów obie listy mają wspólnych
     * @param list1
     * @param list2
     * @return
     */
    public static int countCommonObjects(
            List<?> list1,
            List<?> list2) {
        
        int common = 0;
        for (Object obj : list1) {
            if (list2.contains(obj)) {
                common++;
            }
        }
        return common;
    }

    public static Properties readProperties(String filename) {
        
        Properties properties = new Properties();
        try {
            FileInputStream in = new FileInputStream(filename);
            properties.load(in);
            in.close();
        }
        catch (IOException e) {
            System.err.println("Błąd przy czataniu pliku z ustawieniami");
            e.printStackTrace();
            System.exit(1);
        }
        return properties;
    }

    public static void printAssertionState() {
        try {
            assert false;
            System.out.println("ASERCJE WYŁĄCZONE");
        }
        catch (AssertionError e1) {
            System.err.println("ASERCJE WŁĄCZONE");
        }
    }
    
    /*
    public static <T extends BasicAgent<?>> String agentsValuesToString(List<T> elitists) {

        StringBuffer sb = new StringBuffer();
        for (BasicAgent<?> agent : elitists) {
            sb.append(agent.getValues().get(0)+" "+agent.getValues().get(1)+"\n");
        }
        return sb.toString();
    }
    public static <Genotype> String populationValuesToString(List<Individual<Genotype>> population) {
        StringBuffer sb = new StringBuffer();
        for(Individual<Genotype> ind : population) {
            sb.append(ind.toString()).append("\n");
        }
        return sb.toString();
    }
    
    public static <Genotype> String populationToString(List<Individual<Genotype>> population) {
        
        StringBuffer sb = new StringBuffer();
        for (Individual<Genotype> ind : population) {
            
            List<Double> values = ind.getValues();
            
            sb.append(values.get(0)+" "+values.get(1)+" ");
            sb.append(ind.getGenotype());
            sb.append("\n");
        }
        return sb.toString();
    }
    */

}
